package com.unohacha.qqj.fzdr.ui.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.widget.common.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.bean.CatData;
import com.unohacha.qqj.fzdr.bean.ImgSearchResult;
import com.unohacha.qqj.fzdr.main.FindDetailActivity2;
import com.unohacha.qqj.fzdr.ui.activity.MyImgLibDetialActivity;
import com.unohacha.qqj.fzdr.ui.activity.MyImgLibraryActivity;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.SpaceItemDecoration;
import com.unohacha.qqj.mylibrary.base.BaseFragment;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

import static android.content.Context.MODE_PRIVATE;

/**
 *
 */
public class FindImgDetialFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener{

    //    @BindView(R.id.image_grid_layout)
//    MyGridView imageGridLayout;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private String type="1";
    private int a;
    private List<ImgSearchResult> list,list1;
    SharedPreferences sharedPreferences;
    private String supplyCateId,imgUrl;
    @Override
    protected void initBundle(Bundle arguments) {
        type=arguments.getString("type");
        imgUrl=arguments.getString("imgUrl");
        supplyCateId=arguments.getString("supplyCateId");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find_img_detial;
    }

    @Override
    protected void initView(View rootView) {
        list = new ArrayList<>();
        list1 = new ArrayList<>();
        sharedPreferences=getActivity().getSharedPreferences("locationInfo",MODE_PRIVATE);
//        imageGridLayout.setOnItemClickListener(this);
        WindowManager manager = getActivity().getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        a = outMetrics.widthPixels - 50;
        GridLayoutManager layoutManage = new GridLayoutManager(getContext(), 2);
        recycleview.setLayoutManager(layoutManage);
        recycleview.addItemDecoration(new SpaceItemDecoration(20));
        getData();
    }

    @Override
    protected void initData() {

    }
    private class MyAdapter extends BaseQuickAdapter<ImgSearchResult, BaseViewHolder> {

        public MyAdapter(int layoutResId, @Nullable List<ImgSearchResult> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ImgSearchResult item) {
            Log.d("userinfo",item.getId()+"  "+item.getImg()+"  "+item.getMember_no()+"  "+item.getMember_thumb()+"  "+item.getScore()+"  "+item.getTitle());
            ImageView imageView = (ImageView) helper.getView(R.id.supply_item_type_image);
            final ImageView imgHead = (ImageView) helper.getView(R.id.img_head);
            imageView.setMinimumWidth(a / 2);
            imageView.setMinimumHeight(a / 2);
            Glide.with(FindImgDetialFragment.this)
                    .load(item.getImg())
//                    .error(R.mipmap.ic_default_photo)
                    .override(a / 2, a / 2)
                    .into(imageView);
//            Glide.with(FindImgDetialFragment.this)
//                    .load(item.getMember_thumb())
//                    .error(R.mipmap.ic_default_photo)
//                    .into(imgHead);

            //加载圆形头像
            Glide.with(getActivity()).load(item.getMember_thumb())
                    .asBitmap().centerCrop().into(new BitmapImageViewTarget(imgHead) {
                @Override
                protected void setResource(Bitmap resource) {
                    RoundedBitmapDrawable circularBitmapDrawable =
                            RoundedBitmapDrawableFactory.create(getActivity().getResources(), resource);
                    circularBitmapDrawable.setCircular(true);
                    imgHead.setImageDrawable(circularBitmapDrawable);
                }
            });
            helper.setText(R.id.supply_item_type_text, item.getMember_no());
            helper.setText(R.id.tv_user_name, item.getMember_no());
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(type.equals("1")){
            if(list==null || list.size()==0){
                return;
            }
            String typeId=list.get(position).getSupply_cate_id();
            String mId=list.get(position).getId();
            Intent intent=new Intent(getContext(), FindDetailActivity2.class);
            intent.putExtra("typeid",Integer.valueOf(typeId));
            intent.putExtra("id",Integer.valueOf(mId));
            startActivity(intent);
        }else {
            if(list1==null || list1.size()==0){
                return;
            }
            Prefer.getInstance().setNick(list1.get(position).getMember_no());
            Prefer.getInstance().setMember_id(list1.get(position).getMember_id());
            Prefer.getInstance().setMember_thumb(list1.get(position).getMember_thumb());
            String typeId=list1.get(position).getSupply_cate_id();
            String mId=list1.get(position).getId();
//            Intent intent=new Intent(getContext(), MyImgLibraryActivity.class);
            Intent intent=new Intent(getContext(), MyImgLibDetialActivity.class);
            intent.putExtra("typeid",Integer.valueOf(typeId));
            intent.putExtra("id",Integer.valueOf(mId));
            startActivity(intent);
            Log.d("detaleInfo","id:"+mId+" typeId:"+typeId+" "+Prefer.getInstance().getToken());
        }

    }
    private void getData(){
      //  showLoading();
        String cityId=Prefer.getInstance().obtainReLocation(getContext());
        String token= Prefer.getInstance().getToken();
        SuperHttp.post("api/searchpic/searchPic")
                .addParam("token", token)
                .addParam("type","1")
                .addParam("supply_cate_id",supplyCateId)
                .addParam("city_id",cityId)
                .addParam("img_url",imgUrl)
                .request(new SimpleCallBack<BaseBean<List<ImgSearchResult>>>() {
                    @Override
                    public void onSuccess(BaseBean<List<ImgSearchResult>> data) {
                        if(data.getCode()==0){
                            ToastUtil.showCustomToast("暂无数据");
                            return;
                        }
                     //   list.clear();
                        if(type.equals("1")){
                            for(int i=0;i<data.getData().size();i++){
                                if(data.getData().get(i).getTypeid().equals("1") || data.getData().get(i).getTypeid().equals("2")){
                                    list.add(data.getData().get(i));
                                }
                            }

                            MyAdapter adapter = new MyAdapter(R.layout.item_grid_img_detial, list);
                            adapter.setOnItemClickListener(FindImgDetialFragment.this);
                            recycleview.setAdapter(adapter);
                            if (mRefreshLayout.isRefreshing()) {
                                mRefreshLayout.finishRefresh();
                            }

                        }else {
                            for(int i=0;i<data.getData().size();i++){
                                if(data.getData().get(i).getTypeid().equals("3") ){
                                    list1.add(data.getData().get(i));
                                }
                            }

                            MyAdapter adapter = new MyAdapter(R.layout.item_grid_img_detial, list1);
                            adapter.setOnItemClickListener(FindImgDetialFragment.this);
                            recycleview.setAdapter(adapter);
                            if (mRefreshLayout.isRefreshing()) {
                                mRefreshLayout.finishRefresh();
                            }
                        }


                 //       hideLoading();
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                 //       hideLoading();
                    }
                });
    }
}
